package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.Constants;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.BizCategory;
import com.puboot.module.admin.model.BizContact;
import com.puboot.module.admin.model.User;
import com.puboot.module.admin.service.BizContactService;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.Customer;
import com.puboot.module.blog.service.CustomerService;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("customer")
@AllArgsConstructor
public class CustomerController {

    private final CustomerService customerService;


    @PostMapping("list")
    @ResponseBody
    public PageResultVo loadContactList(Customer customer, Integer pageNumber, Integer pageSize) {
        IPage<Customer> bizCustomerIPage = customerService.pageCustomerAll(customer, pageNumber, pageSize);
        return ResultUtil.table(bizCustomerIPage.getRecords(), bizCustomerIPage.getTotal());
    }

    @PostMapping("/update")
    @ResponseBody
    public ResponseVo edit(String id, Integer state) {
        String msg = state == 0 ? "禁用" : "启用";
        Customer customer = customerService.getById(id);
        customer.setState(state);
        customer.setUpdateTime(new Date());
        boolean i = customerService.updateById(customer);
        if (i) {
            return ResultUtil.success(msg + "用户成功");
        } else {
            return ResultUtil.error(msg + "用户失败");
        }
    }

    @PostMapping("/batch/update")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") Integer[] ids, Integer state) {
        String msg = state == 0 ? "禁用" : "启用";
        int i = customerService.updateStateByIds(state, Arrays.asList(ids));
        if (i > 0) {
            return ResultUtil.success(msg + "用户成功");
        } else {
            return ResultUtil.error(msg + "用户失败");
        }
    }
}
